class Comment < ActiveRecord::Base
  belongs_to :category, :foreign_key=>"category_id"
  belongs_to :user, :foreign_key=>"user_id", :counter_cache => true, :conditions=>"id!=0"
  belongs_to :incumbent, :foreign_key=>"obj_id",:counter_cache =>"reply_times"
  belongs_to :report, :foreign_key=>"obj_id",:counter_cache =>"reply_times"
  belongs_to :company, :foreign_key=>"obj_id",:counter_cache =>"reply_times"
  belongs_to :topic, :foreign_key=>"obj_id",:counter_cache =>"reply_times"
  
  def self.validate_params(params)
    if params[:title]==nil || (params[:title].length>30 || params[:title]=="")
      return ["<li>对不起，回复标题过长或者为空！</li>"]
    elsif params[:body]=="" || params[:body]==nil
      return ["<li>对不起，您的回复内容不能为空！</li>"]
    else
      return []
    end
  end
  
  def after_destory
    obj=eval(category.name).find_by_id(obj_id)
    obj.reply_times-=1
    obj.save
  end
  
end
